/* eslint-disable no-useless-return */
import Vue from 'vue'
import Vuex from 'vuex'

// 导入本地存储
import { setItem, getItem } from '@/utils/index.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // singerMusic: []
    // 存放当前音乐id
    // JSON.parse(window.localStorage.getItem('current-music')) ||
    currentMusic: getItem('current-music') || {},
    showTab: true, // 控制底部标签栏的显示状态
    playList: getItem('play-music') || [], // 存放歌单列表
    // 一个对象，存储当前登录用户信息（token等数据）
    user: getItem('Token')
  },
  mutations: {
    // 获取用户信息
    setUser (state, data) {
      state.user = data

      // 为了防止刷新丢失，我们需要把数据备份到本地存储
      setItem('Token', state.user)
      // window.localStorage.setItem(TOKEN_KEY, JSON.stringify(state.user))
    },
    // 获得当前音乐事件对象
    getCurrentMusic (state, e) {
      state.currentMusic = e

      // 本地持久化存储

      setItem('current-music', state.currentMusic)

      // 存储音乐歌单
      const findResult = state.playList.find(x => x.id === e.id)

      const flag = false
      console.log()
      if (!findResult) {
        state.playList.push(e)
      } else {
        return flag
      }

      // 本地持久化存储

      for (let i = 0; i < state.playList.length; i++) {
        if (state.playList[i] == null || state.playList[i] === '' || JSON.stringify(state.playList[i]) === '{}') {
          state.playList.splice(i, 1)
          console.log(state.playList)
          i = i - 1
        }
      }

      console.log('state.playList=>', state.playList)
      setItem('play-music', state.playList)
    },
    // 判断有没有底部导航栏
    getShowTab (state, e) {
      state.showTab = e
      console.log(e)
    },

    // 获取歌单列表
    removePlayList (state, item) {
      const findResult = state.playList.filter(x => x.id !== item.id)
      console.log(findResult)
      state.playList = findResult

      // 本地持久化存储
      console.log(state.playList)
      const playList = state.playList.filter(item => item.id)
      setItem('play-music', playList)
    }
  },
  actions: {
  },
  modules: {
  }
})
